//gcc myDevice4.c -o myDevice -L/opt/DLAN/lib/ -lixml -lthreadutil -lupnp
#include <stdio.h> 
#include <string.h>
#include "../include/upnp/upnp.h"
#include "../include/upnp/ixml.h"
#include "../include/upnp/upnptools.h"

#define DESC_URL_SIZE 200
////////////////////////////////////////////////////////////////////////////////
int CallbackFun(Upnp_EventType EventType, void* Event, void* Cookie);

////////////////////////////////////////////////////////////////////////////////
int myDeviceStateTableInit(char *DescDocURL);
char *getFirstDocumentItem(IXML_Document *doc, const char *item);

int main(int argc,char* argv[]){

  	int ret;
	int default_advr_expire = 80;

  //  初始化sdk 
  ret = UpnpInit(NULL,0);
  if(UPNP_E_SUCCESS != ret){
    printf("Init uPnP failed!\n");
    UpnpFinish();
  }  
  //  设置根目录
  char web_dir_path[]    = "./chroot";
  if((ret = UpnpSetWebServerRootDir(web_dir_path)) != UPNP_E_SUCCESS){
    printf("Error specifying webserver root directory -- %s:%d\n",web_dir_path,ret);
    UpnpFinish();
  }
  char desc_doc_url[DESC_URL_SIZE];
  const char* ip_address = UpnpGetServerIpAddress();
  const char *desc_doc_name = "ddd.xml";  
  unsigned short port    = UpnpGetServerPort();
  UpnpDevice_Handle device_handle = -1;
  snprintf(desc_doc_url, DESC_URL_SIZE, "http://%s:%d/%s", ip_address, port, desc_doc_name);
  //  注册一个设备
  if((ret = UpnpRegisterRootDevice(desc_doc_url, 
				    CallbackFun, 
				 &device_handle,
				 &device_handle))!=UPNP_E_SUCCESS){
    printf("------------------------------------------registering the rootdevice:%d---------------------------------------------\n", ret);
    UpnpFinish();
    return ret;
  }else{
    printf("-----------------------------------------------RootDevice Registered-------------------------------------------------\n");
   // while(1){
		sleep(5);
		printf("yyyyyyyyyy\n");
 		myDeviceStateTableInit(desc_doc_url);
		ret = UpnpSendAdvertisement(device_handle, default_advr_expire);
		if (ret != UPNP_E_SUCCESS) {
			printf("Error sending advertisements : %d\n", ret);
			UpnpFinish();

			return ret;
		}
		printf("xxxxxxxxxx\n");
	
//	}
    printf("---------------------------------------------State Table Initialized-------------------------------------------------\n");    
  }
	while(1);
  return 0;
}

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
int myDeviceStateTableInit(char *DescDocURL){
  IXML_Document *DescDoc = NULL;
  int ret = UPNP_E_SUCCESS;
//  char *servid_ctrl = NULL;
//  char *:evnturl_ctrl = NULL;
//  char *ctrlurl_ctrl = NULL;
//  char *servid_pict = NULL;
//  char *evnturl_pict = NULL;
//  char *ctrlurl_pict = NULL;
  char *udn = NULL;
  ///下载描述文档///
  if (UpnpDownloadXmlDoc(DescDocURL, &DescDoc) != UPNP_E_SUCCESS) {
    printf("myDeviceStateTableInit -- Error Parsing %s", DescDocURL);
    ret = UPNP_E_INVALID_DESC;
    goto error_handler;
  }
  udn = getFirstDocumentItem(DescDoc, "UDN");

error_handler:
  if (DescDoc){
    ixmlDocument_free(DescDoc);
  }
  return 0;
}

char *getFirstDocumentItem(IXML_Document *doc, const char *item){
  IXML_NodeList *nodeList = NULL;
  IXML_Node *textNode = NULL;
  IXML_Node *tmpNode = NULL;
  char *ret = NULL;
  nodeList = ixmlDocument_getElementsByTagName(doc, (char *)item);
  if (nodeList) {
    tmpNode = ixmlNodeList_item(nodeList, 0);
    if (tmpNode) {
      textNode = ixmlNode_getFirstChild(tmpNode);
      if (!textNode) {
        printf("(BUG) ixmlNode_getFirstChild(tmpNode) returned NULL\n");
        ret = strdup("");
        goto epilogue;
      }
      ret = strdup(ixmlNode_getNodeValue(textNode));
      if (!ret) {
         printf("ixmlNode_getNodeValue returned NULL\n");
         ret = strdup("");
       }
    }else{
      printf("ixmlNodeList_item(nodeList, 0) returned NULL\n");
    }
  }else{
    printf("Error finding %s in XML Node\n", item);
  }
epilogue:
  if (nodeList){
    ixmlNodeList_free(nodeList);
    return ret;
  }
}

int CallbackFun(Upnp_EventType EventType, void* Event, void* Cookie)
{
  return 0;
}

















